package com.cn.wanxi.servlet.product;

import com.alibaba.fastjson.JSONObject;
import com.cn.wanxi.model.ProductModel;
import com.cn.wanxi.model.ResultModel;
import com.cn.wanxi.service.ProductService;
import com.cn.wanxi.service.impl.ProductServiceImpl;
import com.cn.wanxi.util.Tool;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/back/product/getCount")
public class ProductGetCountServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//     1.乱码处理
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
//        2.得到前端的数据
        String name = req.getParameter("name");
        String normalPrice = req.getParameter("normalPrice");//引号里面的是前端对象的key值，小驼峰命名
        String price = req.getParameter("price");
        String content = req.getParameter("content");
        String isShow = req.getParameter("isShow");
        String isRecommend = req.getParameter("isRecommend");
        String imgHref = req.getParameter("imgHref");
        String enable = req.getParameter("enable");
        String whereShow = req.getParameter("whereShow");
        String cuisineId = req.getParameter("cuisineId");
        String style = req.getParameter("style");
//        3.封装数据
        ProductModel productModel = new ProductModel();
        productModel.setName(name);
        productModel.setPrice(Tool.stringToBigDecimal(price));
        productModel.setNormalPrice(Tool.stringToBigDecimal(normalPrice));
        productModel.setContent(content);
        productModel.setIsShow(Tool.stringToInt(isShow));
        productModel.setIsRecommend(Tool.stringToInt(isRecommend));
        productModel.setImgHref(imgHref);
        productModel.setEnable(Tool.stringToInt(enable));
        productModel.setWhereShow(Tool.stringToInt(whereShow));
        productModel.setCuisineId(Tool.stringToInt(cuisineId));
        productModel.setStyle(Tool.stringToInt(style));
//        4.调用服务逻辑层
        ProductService productService=new ProductServiceImpl();
//        5.得到统一格式的结果
        ResultModel resultModel=productService.getCount(productModel);
//        6.将结果返回页面
        resp.getWriter().println(JSONObject.toJSONString(resultModel));


    }
}
